﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EnterpriseFund.Common
{
    /// <summary>
    /// 系统管理基类
    /// </summary>
    public class AdminBaseController : Controller
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //filterContext.HttpContext.Response.AddHeader("X-UA-Compatible", "IE=7");
            //ASPxWebControl.SetIECompatibilityMode(7);
            //#if DEBUG
            //            OperateSession.add(GlobalConstant.PARAM_LOGINID, "admin");
            //            OperateSession.add(GlobalConstant.PARAM_USERNAME, "超级管理员");
            //#endif
            //这里判断是否登录
            if (string.IsNullOrEmpty(OperateSession.get(GlobalConstant.PARAM_LOGINID)))
            {
                Response.Write("<script>window.alert('对不起，请重新登陆！');window.top.location.replace('/EnterpriseFund/Login')</script>");
                Response.Flush();
                Response.End();
            }
        }

        public void WorkFlow(string tableName, string seq, string no, string type, string FNote)
        {
            try
            {
                BLL.WorkFlowProcess workFlowProcessBLL = new BLL.WorkFlowProcess();
                Model.WorkFlowProcess wpModel = new Model.WorkFlowProcess();
                wpModel.FEntityNo = GlobalConstant.ENTITYNO;
                wpModel.FDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                wpModel.FEmplNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                wpModel.FInsIP = GenerateUtil.GetClientIPv4Address();
                wpModel.FNote = FNote;
                wpModel.FProcDate = DateTime.Now;
                wpModel.FSeq = int.Parse(seq);
                wpModel.FSourceNo = no;
                wpModel.FTableName = tableName;


                Model.WorkFlowToDo wtModel = new Model.WorkFlowToDo();

                wtModel.FEntityNo = GlobalConstant.ENTITYNO;
                wtModel.FSourceNo = no;
                wtModel.FSubmitDate = DateTime.Now;
                wtModel.FSubmitDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                wtModel.FSubmitEmplNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                wtModel.FSubmitIP = GenerateUtil.GetClientIPv4Address();
                wtModel.FTableName = tableName;

                string nextNo = "";
                string lastNo = "";
                switch (type)
                {
                    case "return":
                        wpModel.FStatus = "R";
                        wtModel.FStatus = "T";
                        lastNo = workFlowProcessBLL.GetLastNo(tableName, seq);
                        if (lastNo != "-1")
                        {
                            wtModel.FSeq = int.Parse(lastNo);
                        }
                        else
                        {
                            wtModel = null;
                        }
                        break;
                    case "open":
                        wpModel.FStatus = "A";
                        wtModel.FStatus = "N";
                        nextNo = workFlowProcessBLL.GetNextNo(tableName, seq);
                        if (nextNo != "-1")
                        {
                            wtModel.FSeq = int.Parse(nextNo);
                        }
                        else
                        {
                            wtModel = null;
                        }
                        break;
                    case "start":
                        wpModel.FStatus = "S";
                        wtModel.FStatus = "N";
                        nextNo = workFlowProcessBLL.GetNextNo(tableName, seq);
                        if (nextNo != "-1")
                        {
                            wtModel.FSeq = int.Parse(nextNo);
                        }
                        else
                        {
                            wtModel = null;
                        }
                        break;
                }

                workFlowProcessBLL.Add(wpModel, wtModel);
            }
            catch
            {
 
            }
        }
    }

    /// <summary>
    /// 菜单是否授权判断
    /// </summary>
    public class AuthorityActionFilter : ActionFilterAttribute
    {
        /// <summary>
        /// 菜单编码
        /// </summary>
        public string MnuName;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mnuName">菜单编码</param>
        public AuthorityActionFilter(string mnuName)
        {
            MnuName = mnuName;
        }
        /// <summary>
        /// 在action method之前执行
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!string.IsNullOrEmpty(OperateSession.get(GlobalConstant.PARAM_LOGINID)))
            {
                if (!estimateUser(MnuName))
                {
                    filterContext.Controller.ViewBag.Error = "未授权";
                    filterContext.HttpContext.Response.Write("未授权");
                    filterContext.HttpContext.Response.Flush();
                    filterContext.HttpContext.Response.End();
                }
            }
        }

        /// <summary>
        /// 判断权限
        /// </summary>
        /// <param name="strFunctNo">菜单编号</param>
        protected bool estimateUser(string strFunctNo)
        {
            if (OperateSession.get(GlobalConstant.PARAM_LOGINID) != null)
            {
                string strLogID = OperateSession.get(GlobalConstant.PARAM_LOGINID);
                string strEntityNo = GlobalConstant.ENTITYNO;
                return estimateUserPage(strLogID, strEntityNo, strFunctNo);
            }
            return false;
        }

        //判断用户是否有权限操作该页面
        private bool estimateUserPage(string strLogid, string strEntityNo, string strFunctNo)
        {
            strFunctNo = strFunctNo.Trim();
            string strGroupNo = getPrvUserGroup(strLogid, strEntityNo);
            if (strGroupNo.Trim() == "")
            {
                //Response.Redirect("~/WuQuanCaoZuo.aspx");
            }
            else
            {
                BLL.PrvFunctList functlistBll = new BLL.PrvFunctList();
                List<string> functNoList = functlistBll.getFunctNo(strGroupNo, strEntityNo);
                strFunctNo = strFunctNo.Replace("m_acc", "");
                strFunctNo = strFunctNo.Replace("m_", "");
                bool isfind = false;
                for (int i = 0; i < functNoList.Count; i++)
                {
                    functNoList[i] = functNoList[i].Replace("m_acc", "");
                    functNoList[i] = functNoList[i].Replace("m_", "");
                    if (functNoList[i] == strFunctNo)
                    {
                        isfind = true;
                        break;
                    }
                }
                //if (!functNoList.Contains(strFunctNo))//查询出的所拥有的操作菜单中无该页面的菜单
                //{
                //    Response.Redirect("~/WuQuanCaoZuo.aspx");
                //}
                if (!isfind)//查询出的所拥有的操作菜单中无该页面的菜单
                {
                    /// Response.Redirect("~/WuQuanCaoZuo.aspx");
                    return false;
                }

            }

            return true;
        }
        //获取该用户所属的组ID
        public string getPrvUserGroup(string strLogid, string strEntityNo)
        {
            BLL.PrvUserGroup usergroupBll = new BLL.PrvUserGroup();
            List<string> groupNoS = usergroupBll.getListbyFlogidAndEntityNo(strLogid, strEntityNo);
            string strGroupNoS = "";
            foreach (string str in groupNoS)
            {
                strGroupNoS += "'" + str + "',";
            }
            if (strGroupNoS != "")
            {
                strGroupNoS = strGroupNoS.Substring(0, strGroupNoS.Length - 1);
            }

            return strGroupNoS;
        }

    }



    /// <summary>
    /// 记录页面日志
    /// </summary>
    public class LogPageActionFilter : ActionFilterAttribute
    {
        public string PageTitle = "";
        /// <summary>
        /// 页面名称
        /// </summary>
        /// <param name="pageTitle"></param>
        public LogPageActionFilter(string pageTitle)
        {
            PageTitle = pageTitle;
        }
        /// <summary>
        /// 在action method之前执行
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string note = "";
            if (filterContext.Controller.ViewBag.Error != null)
            {
                note = filterContext.Controller.ViewBag.Error;
            }

            string actionParams = "";
            foreach (KeyValuePair<string, object> actionParam in filterContext.ActionParameters)
            {
                actionParams += actionParam.Key + "=" + actionParam.Value + ",";
            }
            if (actionParams != "")
            {
                actionParams = actionParams.Substring(0, actionParams.LastIndexOf(','));
            }

            string visitAddress = HttpContext.Current.Request.ApplicationPath;

            OperateLogHelper.setOperateLog(PageTitle, "", visitAddress, actionParams);
        }
    }
}